//
//  LoginViewController.m
//  Luxury
//
//  Created by DCQ on 2018/5/18.
//  Copyright © 2018年 ZNXZ. All rights reserved.
//

#import "LoginViewController.h"
#import "UserInfoModel.h"
#import "AuthLoginViewController.h"
#import "UIButton+Style.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UIView *accountBgV;
@property (weak, nonatomic) IBOutlet UIView *passwordBgV;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
@property (weak, nonatomic) IBOutlet UIButton *verificationCodeBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
@property (weak, nonatomic) IBOutlet UIButton *smsButton;
@property (nonatomic) NSTimer *sendTimer;
@property (nonatomic) NSDate  *fireDate;
@property (copy, nonatomic) NSString *openid;
@property (copy, nonatomic) NSString *platform;
@property (weak, nonatomic) IBOutlet UIButton *wxBtn;
@property (weak, nonatomic) IBOutlet UIButton *qqBtn;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.accountTF.text = @"13777444628";
    self.passwordTF.text = @"1234";
    [self confighNavRight];
    self.sendTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(sendSms) userInfo:nil repeats:YES];
}
- (BOOL)isShowBackItem{
    return NO;
}
- (void)configView{
    self.accountBgV.layer.masksToBounds = YES;
    self.accountBgV.layer.borderWidth = 1;
    self.accountBgV.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.accountBgV.layer.cornerRadius = 40/2;
    
    
    self.passwordBgV.layer.masksToBounds = YES;
    self.passwordBgV.layer.borderWidth = 1;
    self.passwordBgV.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.passwordBgV.layer.cornerRadius = 40/2;
    
    self.loginBtn.layer.masksToBounds = YES;
    self.loginBtn.layer.borderWidth = 1;
    self.loginBtn.layer.borderColor =  UIColorFromRGB(0xE0E0E0).CGColor;
    self.loginBtn.layer.cornerRadius = 45/2;
    
    [self.wxBtn setupButtonContentType:ButtonContentType_imageCenter];
    [self.qqBtn setupButtonContentType:ButtonContentType_imageCenter];
}
- (void)confighNavRight{
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"login_colse"] forState:UIControlStateNormal];
    [rightBtn setFrame:CGRectMake(0, 0, 40, 40)];
    [rightBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
}
- (void)backAction:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
    if (self.exitCancelBlock) {
        self.exitCancelBlock();
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)getNavBarBgHidden{
    return YES;
}

- (BOOL)getTranslucent{
    return YES;
}
- (UIRectEdge)getViewRect{
    return UIRectEdgeAll;
}

- (IBAction)weixinLoginAction:(id)sender {
    self.platform = @"wx";
     self.openid = @"";
    [[MainApi sharedInstance] postPath:@"User/thirdlogin" params:@{@"openid":self.openid,@"platform": self.platform} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        NSLog(@"%@===",responseObject);
        if (responseObject) {
            NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
            if (userinfo) {
                 [self saveUserInfo:userinfo];
            }
            if (self.successBlock) {
                self.successBlock();
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self gotoAuthLogin];
        }
        
    }];
}

- (IBAction)qqLoginAction:(id)sender {
    self.platform = @"qq";
    self.openid = @"";
    [[MainApi sharedInstance] postPath:@"User/thirdlogin" params:@{@"openid":self.openid,@"platform":@"qq"} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        if (responseObject) {
            NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
            if (userinfo) {
                [self saveUserInfo:userinfo];
            }
            if (self.successBlock) {
                self.successBlock();
            }
            [self dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self gotoAuthLogin];
        }
        
    }];
}
- (IBAction)loginAction:(id)sender {
    
    [[MainApi sharedInstance] postPath:@"User/mobilelogin" params:@{@"mobile":self.accountTF.text,@"captcha":self.passwordTF.text} resultBlock:^(NSDictionary *responseObject, NSError *error) {
//        NSLog(@"%@===",responseObject);
        NSDictionary * userinfo  = responseObject[@"data"][@"userinfo"];
        if (userinfo) {
            [self saveUserInfo:userinfo];
        }
        if (self.successBlock) {
            self.successBlock();
        }
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}

- (void)saveUserInfo:(NSDictionary *)userinfo{
   
    UserInfo.avatar_url = [self filterEmpty:userinfo[@"avatar"]];
    UserInfo.userToken =   [self filterEmpty:userinfo[@"token"]];
    UserInfo.userId = [self filterEmpty:userinfo[@"user_id"]];
    UserInfo.nickname = [self filterEmpty:userinfo[@"nickname"]];
    UserInfo.mobile = [self filterEmpty:userinfo[@"mobile"]];
    UserInfo.gender =  [self filterEmpty:userinfo[@"gender"]];
    UserInfo.birthday = [self filterEmpty:userinfo[@"birthday"]];
    UserInfo.redpack_num = [self filterEmpty:userinfo[@"redpack_num"]];
    UserInfo.coupon_num =  [self filterEmpty:userinfo[@"coupon_num"]];
    UserInfo.wallet_balance = [self filterEmpty:userinfo[@"wallet_balance"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"USER_LOGIN_SUCCESSFUL" object:nil];
 
}
- (NSString *)filterEmpty:(id)parameter{
    NSString * str = @"";
    if (parameter &&![parameter isEqual:[NSNull null]] ) {
        str = parameter;
    }else{
        str = @"";
    }
    return str;
}
- (IBAction)smsAction:(id)sender {
    WEAKSELF
    [[MainApi sharedInstance] postPath:@"Sms/send" params:@{@"mobile":self.accountTF.text} resultBlock:^(NSDictionary *responseObject, NSError *error) {
        STRONGSELF
        NSLog(@"%@===",responseObject);
        strongSelf.fireDate = [NSDate date];
        [strongSelf.sendTimer setFireDate:[NSDate date]];
    }];
    
}

- (void)sendSms {
    NSDate *sDate = self.fireDate;
    NSTimeInterval t = [[NSDate date] timeIntervalSinceDate:sDate];
    NSInteger timeT = 60 - t;
    if (timeT <= 0)
    {
        [self.smsButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.smsButton setUserInteractionEnabled:YES];
        [self.sendTimer setFireDate:[NSDate distantFuture]];
     
        [self.smsButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.smsButton setBackgroundColor:UIColorFromRGB(0xD8CBA5)];
        return ;
    }
    self.smsButton.userInteractionEnabled = NO;
    [self.smsButton setTitle:[NSString stringWithFormat:@"%zd秒", timeT] forState:UIControlStateNormal];
    [self.smsButton setBackgroundColor:UIColorFromRGB(0xcdd2da)]; 
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.sendTimer = nil;
    self.fireDate = nil;
}

- (void)gotoAuthLogin{
    AuthLoginViewController * authLoginVC = [[ AuthLoginViewController alloc]initWithNibName:NSStringFromClass([AuthLoginViewController class]) bundle:nil];
    authLoginVC.openid = self.openid;
    authLoginVC.platform = self.platform;
    [self pushViewController:authLoginVC];
}
@end
